package com.titaniume.rqbbitmq.five;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.titaniume.rqbbitmq.util.RabbitMqUtils;

/**
 * @program: rqbbitmq-hello
 * @description: 消费者02
 * @author: titaniume
 * @create: 2021-12-22 11:34
 */
public class ReceiveLogs02 {

    public  static  final   String EXCHANGE_NAME ="logs";

    public static void main(String[] args) throws  Exception {
        Channel channel = RabbitMqUtils.getChannel();
        //声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");
        //声明一个队列 临时队列
        //队列的名称是随机的 消费者断开与队列的链接后自动删除
        String queueName = channel.queueDeclare().getQueue();
        //把该临时队列绑定我们的exchanges 其中routingkey（也成为binding key）为空字符串
        channel.queueBind(queueName,EXCHANGE_NAME,"");
        System.out.println("02等待接收消息，把接收的消息打印到控制台...");
        //接收消息
        DeliverCallback deliverCallback =(consumerTag,message) ->{
            System.out.println("02控制台打印接收到的消息：" + new String(message.getBody(),"UTF-8"));
        };
        channel.basicConsume(queueName,true,deliverCallback,consumerTag ->{ });
    }
}